<template>
  <div class="data-energy-wrapper">
    <xyDataHead :showDetail="false">
      <template #icon>
        <svg-icon :icon-class="svgIcon" class="svg-icon"></svg-icon>
      </template>
      <template #title>
        <p>{{ title }}</p>
      </template>
      <template #number>
        <div class="warn-number" v-show="num">{{ num }}</div>
      </template>
    </xyDataHead>
    <div class="data-energy-container" ref="chartContainer"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import xyDataHead from "./xy-data-head.vue";
export default {
  components: {
    xyDataHead,
  },
  props: {
    title: {
      type: String,
      default: "",
    },
    svgIcon: {
      type: String,
      default: "",
    },
    num: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
      chartData: {
        xAxis: [
          "00:00",
          "03:00",
          "06:00",
          "09:00",
          "12:00",
          "15:00",
          "18:00",
          "21:00",
        ],
        series1: [21, 20, 20, 22, 24, 24, 23, 22], // 设定温度
        series2: [20.5, 19.8, 19.5, 21.8, 23.5, 23.2, 22.8, 21.9], // 测量温度
      },
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      window.removeEventListener("resize", this.resizeChart);
    }
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        const chartDom = this.$refs.chartContainer;
        if (chartDom) {
          this.chart = echarts.init(chartDom);
          this.setChartOption();

          window.addEventListener("resize", this.resizeChart);
        }
      });
    },

    setChartOption() {
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            lineStyle: {
              color: "rgba(255, 255, 255, 0.3)",
              width: 1,
              type: "dashed",
            },
            crossStyle: {
              color: "rgba(255, 255, 255, 0.2)",
            },
          },
          backgroundColor: "rgba(26, 32, 44, 0.95)",
          borderColor: "rgba(59, 130, 246, 0.5)",
          borderWidth: 1,
          textStyle: {
            color: "#ffffff",
            fontSize: 12,
          },
          padding: [12, 15],
          borderRadius: 6,
          formatter: (params) => {
            let result = `${params[0].axisValue}<br/>`;
            let deviation = Math.abs(params[0].value - params[1].value);
            let status =
              deviation < 0.5
                ? "正常"
                : deviation < 1
                ? "轻微偏差"
                : "较大偏差";
            let statusColor =
              deviation < 0.5
                ? "#10b981"
                : deviation < 1
                ? "#f59e0b"
                : "#ef4444";

            params.forEach(function (param) {
              result += `${param.marker}${param.seriesName}: <strong>${param.value}°C</strong><br/>`;
            });
            result += `<span style="color: ${statusColor};">偏差状态: ${status}</span>`;
            return result;
          },
        },
        legend: {
          data: [
            {
              name: "设定温度",

              textStyle: {
                color: "#94a3b8",
              },
            },
            {
              name: "测量温度",

              textStyle: {
                color: "#94a3b8",
              },
            },
          ],
          textStyle: {
            color: "#94a3b8",
            fontSize: 12,
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chartData.xAxis,
          axisLabel: {
            color: "#94a3b8",
            margin: 10,
            fontSize: 11,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(148, 163, 184, 0.2)",
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          type: "value",
          name: "温度 (°C)",
          nameTextStyle: {
            color: "#94a3b8",
            padding: [0, 0, 0, 40],
            fontSize: 11,
          },
          min: 15,
          max: 30,
          axisLabel: {
            color: "#94a3b8",
            fontSize: 11,
            formatter: "{value}°C",
          },
          splitLine: {
            lineStyle: {
              color: "rgba(148, 163, 184, 0.1)",
            },
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
        },
        series: [
          {
            name: "舒适温度区间",
            type: "line",
            stack: "temperature_band",
            symbol: "none",
            lineStyle: { show: false },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(16, 185, 129, 0.1)" },
                { offset: 1, color: "rgba(16, 185, 129, 0.02)" },
              ]),
            },
            emphasis: { focus: "series" },
            data: [25, 25, 25, 25, 25, 25, 25, 25],
            z: 0,
          },
          {
            name: "舒适温度下限",
            type: "line",
            stack: "temperature_band",
            symbol: "none",
            lineStyle: { show: false },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(26, 32, 44, 1)" },
                { offset: 1, color: "rgba(26, 32, 44, 1)" },
              ]),
            },
            emphasis: { focus: "series" },
            data: [19, 19, 19, 19, 19, 19, 19, 19],
            z: 1,
          },
          {
            name: "设定温度",
            type: "line",
            data: this.chartData.series1,
            smooth: true,
            symbol: "circle",
            symbolSize: 8,

            lineStyle: {
              width: 4,
              color: "#ef4444",
              shadowBlur: 8,
              shadowColor: "rgba(239, 68, 68, 0.4)",
            },
            itemStyle: {
              color: "#ef4444",
              borderWidth: 2,
              borderColor: "#ffffff",
              shadowBlur: 2,
              shadowColor: "rgba(239, 68, 68, 0.5)",
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(239, 68, 68, 0.15)" },
                { offset: 1, color: "rgba(239, 68, 68, 0.02)" },
              ]),
            },
            emphasis: {
              scale: 1.8,
              itemStyle: {
                borderWidth: 3,
                shadowBlur: 12,
                shadowColor: "rgba(239, 68, 68, 0.7)",
              },
            },
            animationDuration: 1800,
            animationEasing: "cubicOut",
            z: 3,
          },
          {
            name: "测量温度",
            type: "line",
            data: this.chartData.series2,
            smooth: true,
            symbol: "circle",
            symbolSize: 8,
            lineStyle: {
              width: 4,
              color: "#3b82f6",
              shadowBlur: 8,
              shadowColor: "rgba(59, 130, 246, 0.4)",
            },
            itemStyle: {
              color: "#3b82f6",
              borderWidth: 2,
              borderColor: "#ffffff",
              shadowBlur: 2,
              shadowColor: "rgba(59, 130, 246, 0.5)",
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(59, 130, 246, 0.15)" },
                { offset: 1, color: "rgba(59, 130, 246, 0.02)" },
              ]),
            },
            emphasis: {
              scale: 1.8,
              itemStyle: {
                borderWidth: 3,
                shadowBlur: 12,
                shadowColor: "rgba(59, 130, 246, 0.7)",
              },
            },
            animationDuration: 2000,
            animationEasing: "cubicOut",
            z: 2,
          },
        ],
        animation: true,
        animationDuration: 1500,
        animationEasing: "cubicOut",
        animationDelay: (idx) => {
          return idx * 50;
        },
      };

      this.chart.setOption(option);
    },

    resizeChart() {
      if (this.chart) {
        setTimeout(() => {
          this.chart.resize();
        }, 100);
      }
    },
  },
  watch: {
    chartData: {
      handler() {
        if (this.chart) {
          this.setChartOption();
        }
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.data-energy-wrapper {
  height: 100%;
  .data-energy-container {
    height: calc(100% - 50px);
    width: 100%;
    position: relative;
    margin-top: 10px;
  }
}
</style>
